package com.myself.ssoserver.controller

import com.myself.common.page.Page
import com.myself.common.page.PageResult
import com.myself.ssoserver.entity.User
import com.myself.ssoserver.entity.Users
import com.myself.ssoserver.service.SessionService
import com.myself.ssoserver.service.UserService
import org.ktorm.dsl.eq
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/user")
class UserController(
    private val userService: UserService,
    private val sessionService: SessionService
) {

    @PostMapping("/findAll")
    fun searchAll(@RequestBody page: Page): PageResult<User> {
        return userService.findAll(page)
    }

    @PostMapping("/updateActive")
    fun updateActive(@RequestBody user: User) {
         userService.updateOneBy(
            condition = { Users.id eq user.id },
            change = { it.isActive = user.isActive }
        )
        if (user.isActive == 1){
            sessionService.clearUserTokens(user.id)
        }
    }


}